package com.yimixian.app.address;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.ta.utdid2.android.utils.StringUtils;
import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.EventBusDomain.ShowAdSelectChange;
import com.yimixian.app.R;
import com.yimixian.app.activity.BaseNaviFragmentGroupActivity;
import com.yimixian.app.adapter.ArrayListAdapter;
import com.yimixian.app.adapter.PoiListViewAdapter;
import com.yimixian.app.adapter.PoiListViewAdapter2;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.common.StatConstants;
import com.yimixian.app.common.YmxTitleBarActivity;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.location.LocatedListener;
import com.yimixian.app.location.LocatedListener2;
import com.yimixian.app.location.LocationProvider;
import com.yimixian.app.model.Address;
import com.yimixian.app.model.AppConfig;
import com.yimixian.app.model.KV;
import com.yimixian.app.model.Region;
import com.yimixian.app.ui.dialog.WheelViewDialog;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.Strings;
import com.yimixian.app.util.UiUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;
import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/5/16 0016.
 */
public class SwitchAddressActivity extends Activity {

    public static final String EXTRA_ADDRESS_ID = "extra_address_id";
    private TextView tv_new_address, tv_city;
    private ListView poiListView;
    private View loading_view;
    private View ll_list;
    private View ll_right;
    private PoiSearch mPoiSearch;
    private ArrayList<PoiInfo> mAroundPoiList = new ArrayList<>();
    private PoiListViewAdapter2 mAroundPoiListViewAdapter;
    private DataManager mDataManager;
    private ImageView iv_back;
    private String mProvince, mCity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch_address);

        loading_view = findViewById(R.id.loading_view);
        ll_list = findViewById(R.id.ll_list);
        ll_right = findViewById(R.id.ll_right);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_new_address = (TextView) findViewById(R.id.tv_new_address);
        tv_city = (TextView) findViewById(R.id.tv_city);
        poiListView = (ListView) findViewById(R.id.poi_list_view);

        tv_city.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showListItemDialog("选择城市");
            }
        });

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tv_new_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                MobclickAgent.onEvent(SwitchAddressActivity.this,
//                        StatConstants.CHOOSE_ADDRESS_ADD_NEW_ADDRESS_CLICK);
//                Intent intent = new Intent(SwitchAddressActivity.this,
//                        ModifyOrCreateAddressActivity.class);
//                startActivityForResult(intent, Constants.MODIFY_ADDRESS_REQUEST_CODE);
                        startActivityForResult(
                                new Intent(SwitchAddressActivity.this, ChooseAddressOrStoreActivity.class).putExtra(
                                        ShowAddressesActivity.EXTRA_MODE,
                                        ShowAddressesActivity.LOCATE_ADDRESS_MODE),
                                Constants.PICK_ADDRESS_REQUEST_CODE);
            }
        });

        ll_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(SwitchAddressActivity.this, SwitchSearchActivity.class).putExtra("city", mCity), 0);
                overridePendingTransition(R.anim.activity_slide_right_in, R.anim.activity_anim_stay);
            }
        });

        LocationProvider.getInstance().startBaiduLocated2(false, new LocatedListener2() {

            @Override
            public void locatedSuccessListener(BDLocation location) {

                if (location == null) {
                    ll_list.setVisibility(View.INVISIBLE);
                    UiUtils.showToast("获取数据失败");
                    return;
                }

                mProvince = location.getProvince();
                mCity = location.getCity();

                tv_city.setText(mCity);
                tv_city.setVisibility(View.VISIBLE);

                BaiduMapUtilByRacer.getPoisByGeoCode(location.getLatitude(), location.getLongitude(),
                        new BaiduMapUtilByRacer.GeoCodePoiListener() {
                            @Override
                            public void onGetSucceed(LocationBean locationBean, List<PoiInfo> poiList) {
                                loading_view.setVisibility(View.INVISIBLE);
                                mAroundPoiList.clear();

                                if (poiList == null) {
                                    ll_list.setVisibility(View.INVISIBLE);
                                    UiUtils.showToast("获取数据失败");
                                    return;
                                }

                                for (PoiInfo poiInfo : poiList) {
                                    if (!poiInfo.address.contains(";")) {
                                        mAroundPoiList.add(poiInfo);
                                    }
                                }
                                mAroundPoiListViewAdapter.setList(mAroundPoiList);
                                mAroundPoiListViewAdapter.notifyDataSetChanged();
                                ll_list.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onGetFailed() {
                                loading_view.setVisibility(View.INVISIBLE);
                                ll_list.setVisibility(View.INVISIBLE);
                                UiUtils.showToast("获取数据失败");
                            }
                        });
            }

            @Override
            public void locatedFailedListener() {
                UiUtils.showToast("定位失败");
                loading_view.setVisibility(View.INVISIBLE);
                ll_list.setVisibility(View.INVISIBLE);
            }
        });

        loading_view.setVisibility(View.VISIBLE);

        mPoiSearch = PoiSearch.newInstance();
        //获取检索内容监听
        mPoiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                loading_view.setVisibility(View.INVISIBLE);
                if (poiResult == null || poiResult.getAllPoi() == null) {
                    mAroundPoiList = null;
                } else {
                    mAroundPoiList = new ArrayList<>();
                    for (PoiInfo poiInfo : poiResult.getAllPoi()) {
                        if (!poiInfo.address.contains(";")) {
                            mAroundPoiList.add(poiInfo);
                        }
                    }
                }
                mAroundPoiListViewAdapter.setList(mAroundPoiList);
                mAroundPoiListViewAdapter.notifyDataSetChanged();
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
            }
        });

        mAroundPoiListViewAdapter = new PoiListViewAdapter2(this);
        poiListView.setAdapter(mAroundPoiListViewAdapter);

        poiListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PoiInfo poiInfo = mAroundPoiList.get(position);
                if(poiInfo != null){
                    ShortAddress shortAddress = new ShortAddress(poiInfo, poiInfo.location);
                    SharedPreferencesHelper.saveAddress(shortAddress, SharePreferenceKeys.CURRENT_ADDRESS);
                    ShortAddress shortNoLoginAddress = new ShortAddress(poiInfo, poiInfo.location);
                    SharedPreferencesHelper.saveAddress(shortNoLoginAddress, SharePreferenceKeys.CURRENT_NO_LOGIN_ADDRESS);
                    EventBus.getDefault().postSticky(new ShowAdSelectChange());
                    setResult(RESULT_OK);
                    finish();
                }
            }
        });

    }

    private void showListItemDialog(String title){

        AppConfig appConfig = DataManager.getInstance().get(CacheKeys.APP_CONFIG);
        if (appConfig.regions.size() <= 0)
            return;
        WheelViewDialog wheelViewDialog_age = new WheelViewDialog(this,
                appConfig.regions, mProvince, mCity);
        wheelViewDialog_age.setmClickSureListener(new WheelViewDialog.ClickSureListener() {
            @Override
            public void onConfirm(KV kv) {
            }

            @Override
            public void onTwoSelectConfirm(String province, String city) {
                mProvince = province;
                mCity = city;
                tv_city.setText(mCity);
            }
        });
        wheelViewDialog_age.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK)
            finish();
    }
}
